package OOP.HouseRent;

import java.util.Scanner;

public class HouseService {

    public static Houses[] AddHouse(Houses[] house,int count) {
        // 动态增加
        Scanner scanner = new Scanner(System.in);
        Houses[] houseadd = new Houses[house.length + 1];
        for (int i = 0; i < house.length; i++) {
            houseadd[i] = house[i];
        }
        house = houseadd;

        house[house.length - 1] = new Houses(); // 创建新的 Houses 对象

        System.out.println("----------添加房屋----------");

        house[house.length - 1].setCount(count);

        System.out.print("姓名：");
        String name = scanner.next();
        house[house.length - 1].setName(name);

        System.out.print("电话：");
        long number = scanner.nextLong();
        house[house.length - 1].setNumber(number);

        System.out.print("地址：");
        String address = scanner.next();
        house[house.length - 1].setAddress(address);

        System.out.print("月租：");
        int money = scanner.nextInt();
        house[house.length - 1].setRentmoney(money);

        System.out.print("出租情况（已出租/未出租）：");
        String isranted = scanner.next();
        house[house.length - 1].setIsranted(isranted);

        System.out.println("----------添加完成----------");

        return house;
    }

    public static void FindHouse(Houses[] house) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("----------查找房屋----------");
        System.out.print("请输入你要查找的id:");
        int choice = scanner.nextInt();
        if(choice <= house.length && choice > 0 &&house[choice - 1].getCount()!=0){
            System.out.println(house[choice-1].showinfo());
            System.out.println("----------查找房屋----------");
        }
        else{
            System.out.println("----------没有该房屋----------");
        }

    }

    public static Houses[] RemoveHouse(Houses[] house) {
        while(true){
            System.out.println("----------删除房屋----------");
            System.out.print("请选择待删除房屋编号（-1）退出");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
            if(choice == -1){
                break;
            }
            else if(choice<=0 || choice>house.length || house[choice-1].getCount()==0){
                System.out.println("----------输入有误，请重新输入----------");

            }
            else{
                while(true){
                    System.out.println("确认是否删除（Y/N）：");
                    char choice2 = scanner.next().charAt(0);
                    if(choice2 == 'Y'){
                        house[choice-1].setCount(0);
                        System.out.println("----------删除完成----------");
                        break;
                    }
                    else break;
                }

            }
        }
        return house;

    }

    public static Houses[] reviseHouse(Houses[] house) {
        while(true){
            System.out.println("----------修改客户----------");
            System.out.print("请选择待修改房屋编号（-1）退出");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
            if(choice == -1){
                break;
            }
            else if(choice<=0 || choice>house.length || house[choice-1].getCount()==0){
                System.out.println("----------输入有误，请重新输入----------");

            }
            else{
                System.out.print("姓名（"+house[house.length - 1].getName()+"):");
                String name = scanner.next();
                if(!name.isEmpty()){
                    house[house.length - 1].setName(name);
                }

                System.out.print("电话("+house[house.length - 1].getNumber()+"):");
                String number = scanner.next();
                if(!number.isEmpty()){
                    house[house.length - 1].setNumber(Long.parseLong(number));
                }

                System.out.print("地址("+house[house.length - 1].getAddress()+"):");
                String address = scanner.next();
                if(!address.isEmpty()){
                    house[house.length - 1].setAddress(address);
                }

                System.out.print("月租("+house[house.length - 1].getRentmoney()+"):");
                String money = scanner.next();
                if(!money.isEmpty()){
                    house[house.length - 1].setRentmoney(Integer.parseInt(money));
                }


                System.out.print("出租情况("+house[house.length - 1].getIsranted()+"):");
                String isranted = scanner.next();
                if(!isranted.isEmpty()){
                    house[house.length - 1].setIsranted(isranted);
                }

                System.out.println("----------修改完成----------");
            }


        }
        return house;
    }
    public  static void showhouses(Houses[] house) {
        System.out.println("----------房屋列表----------");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t出租状态");
        for (int i = 0; i < house.length; i++) {
            if(house[i].getCount()!=0){
                System.out.println(house[i].showinfo());
            }
        }
        System.out.println("----------房屋列表完成----------");
    }
}
